#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>

int main(int argc, char *argv[])
{
	int cfd;
	struct sockaddr_in addr, caddr;
	socklen_t len = sizeof(addr);
	char buf[BUFSIZ];
	char buf2[BUFSIZ];

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(8080);
	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

	cfd = socket(AF_INET, SOCK_DGRAM, 0);

	while(fgets(buf, BUFSIZ, stdin)!=NULL)
	{
		sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, len);
		int ret = recvfrom(cfd, buf2, sizeof(buf2), 0, NULL, 0);
		write(STDOUT_FILENO, buf2, ret);
	}
	close(cfd);
	return 0;

}
